package com.sgcc.sx.pmos.e_sx_order.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.sx.pmos.arouter.order.OrderArouterPath;
import com.sgcc.sx.pmos.common.base.BasePresenter;
import com.sgcc.sx.pmos.common.base.MvpBaseActivity;
import com.sgcc.sx.pmos.common.dialog.CommonDialog;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvcNetCallback;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_order.bean.EnergyByMonthDetailBean;
import com.sgcc.sx.pmos.e_sx_order.bean.PeriodPointBean;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 电量变更--不分时段
 */
@Route(path = OrderArouterPath.ORDER_NODAYPARTINGELECHANGES)
public class NoDaypartingEleChangesActivity extends MvpBaseActivity {

    private static final String TAG = "NoDaypartingEleChangesA";

    private TitleBar mTitleBar;
    private EditText mEtTotalAmount;
    private RelativeLayout mRlSubmitChanges;
    private PeriodPointBean mPeriodPointBean;
    private String mRelationId, mPackageId, mMonth;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_nodaypartingelechanges;
    }

    @Override
    protected void receiveIntentExtras() {
        super.receiveIntentExtras();
        mRelationId = getIntent().getStringExtra(ArguConstant.RELATIONID);
        mPackageId = getIntent().getStringExtra(ArguConstant.PACKAGEID);
        mMonth = getIntent().getStringExtra(ArguConstant.MONTH);
    }


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mEtTotalAmount = (EditText) findViewById(R.id.et_total_amount);
        mRlSubmitChanges = (RelativeLayout) findViewById(R.id.rl_submit_changes);

        mTitleBar.setTitle("电量变更");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });

        mRlSubmitChanges.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = mEtTotalAmount.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showShort("请输入约定电量");
                    return;
                }
                mPeriodPointBean.setEnergy0(content);
                mPeriodPointBean.setRelationId(mRelationId);
                String json = GsonUtils.toJson(mPeriodPointBean);
                showPromptDialog(json);
            }
        });

        getEnergyByMonthDetail(mRelationId, mPackageId);
    }

    private void showPromptDialog(String json) {
        CommonDialog dialog = new CommonDialog(mContext);
        dialog.setTitle("提示");
        dialog.setMessage("确认要提交" + mMonth + "月电量变更申请吗？");
        dialog.setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                dialog.dismiss();
                ARouter.getInstance()
                        .build(OrderArouterPath.ORDER_CONFIRMORDER)
                        .withString("json", json)
                        .withString("mRelationId", mRelationId)
                        .withString("monthDate", mPeriodPointBean.getMonthDate())
                        .withInt("pageFlag", 4)
                        .withBoolean("isDayparting", false)
                        .navigation();

            }

            @Override
            public void onNegtiveClick() {
                dialog.dismiss();
            }
        }).show();
    }



    public void getEnergyByMonthDetail(String relationId, String packageId) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("relationId", relationId);
        params.put("packageId", packageId);
        Map<String, String> headers = new HashMap<>();
        headers.put("CurrentRoute", "/pxf-phbsx-shop/#/contractChange");
        HttpRequestUtils.getInstance().requestWithDiffHeaders(params, HttpInterConst.ORDER_GETENERGYBYMONTHDETAIL, new MvcNetCallback<EnergyByMonthDetailBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁

            }

            @Override
            public void onResponse(EnergyByMonthDetailBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if ("200".equals(response.getCode())) {
                        mPeriodPointBean = response.getData().get(0);
                        mEtTotalAmount.setText(response.getData().get(0).getEnergy0());
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }

            }
        }, headers);
    }
}
